﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APA_DAO;
using TAM_SPORT;
using DAO;

namespace AMS
{
    public partial class FrmThuePhongList : Form
    {
        public FrmThuePhongList()
        {
            InitializeComponent();
            loadGrid();
            if (LoginDao.adminFlag)
            {
                clHuy.Visible = true;
            }
            else
            {
                clHuy.Visible = false;
            }
            dtResult.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSkyBlue;
            dtResult.GridColor = Color.BurlyWood;
            dtResult.EnableHeadersVisualStyles = false;
            this.BackColor = Color.LightCyan;
        }

        public FrmThuePhongList(string strSearch)
        {
            InitializeComponent();
            this.txtSearch.Text = strSearch;
            loadGrid();
            if (LoginDao.adminFlag)
            {
                clHuy.Visible = true;
            }
            else
            {
                clHuy.Visible = false;
            }
            dtResult.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSkyBlue;
            dtResult.GridColor = Color.BurlyWood;
            dtResult.EnableHeadersVisualStyles = false;
            this.BackColor = Color.LightCyan;
        }


        private void thayĐổiThôngTinNgườiThuêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmEditMembers f = new FrmEditMembers();
            f.ShowDialog();
            f.Dispose();
        }

        private void thayĐổiThôngTinXeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmEditMoto f = new FrmEditMoto();
            f.ShowDialog();
            f.Dispose();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadGrid();
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private DataGridViewImageCell loadImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                DataGridViewImageCell imageCell = new DataGridViewImageCell();
                Image imagedis = resizeImage(image, new Size(150, 150));
                imageCell.Value = imagedis;
                image.Dispose();
                return imageCell;
            }
            catch
            {
                return new DataGridViewImageCell();
            }
        }

        private void loadGrid()
        {
            dtResult.Rows.Clear();
            ThuePhongDao dao = new ThuePhongDao();
            DataTable table = dao.getThuePhongs(txtSearch.Text);
            int i = 0;            
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    i = dtResult.Rows.Add();
                    dtResult.Rows[i].Cells[0].Value = (i + 1).ToString();
                    dtResult.Rows[i].Cells[1] = row["HINH_ANH"] == null ? null : loadImage(row["HINH_ANH"].ToString());
                    dtResult.Rows[i].Cells[2].Value = row["HO_VA_TEN"].ToString();
                    dtResult.Rows[i].Cells[3].Value = row["SO_PHONG"].ToString();
                    dtResult.Rows[i].Cells[4].Value = row["NGAY_THU"] == System.DBNull.Value ? "" : DateTime.Parse(row["NGAY_THU"].ToString()).ToString("dd/MM/yyyy");
                    dtResult.Rows[i].Cells[5].Value = row["NGAY_KET_THUC_KY_NAY"] == System.DBNull.Value ? "" :  DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()).ToString("dd/MM/yyyy");
                    dtResult.Rows[i].Cells[6].Value = row["ID"].ToString();
                    dtResult.Rows[i].Cells[9].Value = row["MA_PHONG"].ToString();
                    if (row["NGAY_KET_THUC_KY_NAY"].GetType().FullName == "System.DBNull" || DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) <= System.DateTime.Now.AddDays(3))
                    {
                        dtResult.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                    if (row["NGAY_KET_THUC_KY_NAY"].GetType().FullName != "System.DBNull" && System.DateTime.Now.AddDays(3) < DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) && DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) <= System.DateTime.Now.AddDays(5))
                    {
                        dtResult.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
                
            }
        }

        private void dtResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int soHopDong = int.Parse(dtResult.Rows[e.RowIndex].Cells[6].Value.ToString());
                int roomId = int.Parse(dtResult.Rows[e.RowIndex].Cells[9].Value.ToString());
                string spPhong = dtResult.Rows[e.RowIndex].Cells[3].Value.ToString();
                string cusName = dtResult.Rows[e.RowIndex].Cells[2].Value.ToString();
                if (e.ColumnIndex == 0)
                {
                    FrmEditThuePhongInfor f = new FrmEditThuePhongInfor(soHopDong, roomId);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }
                if (e.ColumnIndex == 1)
                {                   
                    FrmThuTienPhong f = new FrmThuTienPhong(soHopDong);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }

                if (e.ColumnIndex == 2)
                {
                    FrmThuTienHist f = new FrmThuTienHist(spPhong,cusName,soHopDong);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }
                if (e.ColumnIndex == 3)
                {
                    FrmEditMembers f = new FrmEditMembers(soHopDong);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }
                if (e.ColumnIndex == 4)
                {
                    FrmEditMoto f = new FrmEditMoto(soHopDong);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }
                if (e.ColumnIndex == 5)
                {
                    FrmTraPhong f = new FrmTraPhong(soHopDong);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();

                }
                
            }
            
        }

        private void dtResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int soHopDong = int.Parse(dtResult.Rows[e.RowIndex].Cells[6].Value.ToString());
                if (e.ColumnIndex == 7)
                {
                    FrmThuTienPhong f = new FrmThuTienPhong(soHopDong, true);
                    f.ShowDialog();
                    f.Dispose();
                    loadGrid();
                }

                if (e.ColumnIndex == 8)
                {
                    Confirm f = new Confirm("Bạn muốn hủy hợp đồng thuê phòng này. \nLưu ý: Tất cả các chứng từ liên quan hợp đồng sẽ được hủy theo.");
                    f.ShowDialog();
                    if (f.IsAccept)
                    {
                        ThuePhongDao dao = new ThuePhongDao();
                        if (dao.huyThuePhong(soHopDong))
                        {
                            Result re = new Result("Hủy thành công");
                            re.ShowDialog();
                            re.Dispose();
                            loadGrid();
                        }
                        else
                        {
                            Error er = new Error("Hủy thất bại.");
                            er.ShowDialog();
                            er.Dispose();
                        }
                    }
                    f.Dispose();
                }
            }
        }
    }
}
